package com.travel.server.manager.impl.command.adversiting;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.advertising.UpdateContentMessageRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.ContentDal;
import com.travel.server.manager.model.ContentInfo;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/20 15:53
 * @Desc:
 */
@Component
@Slf4j
public class UpdateContentMessageCommand extends AbstractCommand<UpdateContentMessageRequest, EmptyResponse> {

    @Autowired
    private ContentDal contentDal;

    @Override
    protected void checkInput(UpdateContentMessageRequest request) {
        if(request.getType() == null){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(UpdateContentMessageRequest request) {
        Date now = new Date();
        UserInfo userInfo = getUserInfo();
        ContentInfo contentInfo = contentDal.select();
        if(contentInfo == null){
            contentInfo = new ContentInfo();
            contentInfo.setCreateBy(userInfo.getName());
            contentInfo.setCreateTime(now);
        }
        Integer type = request.getType();
        String message = request.getMessage();
        if(CommonConstant.ABOUT_US.equals(type)){
            contentInfo.setAboutUs(message);
        }else if(CommonConstant.CONTACT_US.equals(type)){
            contentInfo.setContactUs(message);
        }else if(CommonConstant.JOIN_US.equals(type)){
            contentInfo.setJoinUs(message);
        }else{
            throwInvalidParam();
        }
        contentInfo.setUpdateBy(userInfo.getName());
        contentInfo.setUpdateTime(now);
        contentDal.insertOrUpdate(contentInfo);
        return new EmptyResponse();
    }
}
